﻿<?php

class database {
	private $hostname = "localhost";
	private $username = "root";
	private $password = "";
	private $dbname = "qly_drl";
	private $flagConn = false;
	protected $connection = NULL; 
	protected $query;
	protected $result;
	
	public function __construct($new = false) {
		if (!$this->connection = mysql_connect($this->hostname, $this->username, $this->password, $new)) {
			die("Không thể kết nối với server.");
		} else {
			if (!mysql_select_db($this->dbname, $this->connection)){
				die ("Không thể kết nối với cơ sở dữ liệu.");
				mysql_close($this->connection);
			} else {
				$this->flagConn = true;
				mysql_query("SET NAMES 'utf8'");
			}
		}
	}
	
	public function getConnection() { return $this->connection; }
	
	public function setQuery($query) { $this->query = $query; }
	public function getQuery(){ return $this->query; }
	
	public function setResult($result) { $this->result = $result; }
	public function getResult(){ return $this->result; }
	
	public function isExists($table = "", $where = "") {
		if (empty($table) || empty($where))
			return false;
		
		if ($this->flagConn == false)
			$this->__construct();
		
		$this->query = "select distinct * from $table where $where";
		$this->result = mysql_query($this->query);
		return mysql_num_rows($this->result);
	}
                    
	public function fetchAll() {
		if ($this->flagConn == false)
			$this->__construct();
		
		$this->result = mysql_query($this->query, $this->connection);
		return $this->result;
	}
	
	public function delete($table = "", $where = "") {
		if (empty($table) || empty($where))
			return false;
			
		$this->query = "delete from $table where $where"; 
		$this->result = mysql_query($this->query, $this->connection);
	
		return mysql_affected_rows();
	}

	public function insert($table = "", $atts = "", $duplicate = "") {
		if (empty($table) || !is_array($atts))
			return false;

		$col_str = $val_str = "";
		
		foreach ($atts as $col => $val) {
			if (empty($val) && is_string($val)) continue;
			
			$col_str .= $col . ",";
			
			if (is_int($val) || is_double($val))
				$val_str .= $val . ",";
			else $val_str .= "'$val',";
		}
		
		$query = str_replace(",)", ")", "insert into $table($col_str) values($val_str)");
		
		if (!empty($duplicate))
			$query .= " ON DUPLICATE KEY UPDATE " . $duplicate;
		
		// echo '<p>'.$query.'</p>';
		
		$this->query = $query;
		
		$this->result = mysql_query($this->query, $this->connection);// or die("Lỗi MySQL: " . mysql_error());
		
		return mysql_affected_rows();
	}

	public function update($table = "", $atts = "", $where = "") {
		if (empty($table) || !is_array($atts))
			return false;
			
		while(list($col, $val) = each($atts)) {
			if (empty($val)) continue;
			
			if (is_int($val) || is_double($val))
				$str .= "$col = $val,";
			else if ($val == "NULL" || $val == "null")
				$str .= "$col = NULL,";
			else $str .= "$col = '$val',";
		}
	
		$str = substr($str, 0, -1);
		$this->query = "update $table set $str";
		
		if (!empty($where))
			$this->query .= " where $where";
			
		$this->result = mysql_query($this->query, $this->connection);
		return mysql_affected_rows();
	}
	
	public function select($table = "", $atts = "", $where = "") {
		if (empty($table) || !is_array($atts))
			return false;
			
		$selected = implode(", ", $atts);
		$this->query = "select $selected from $table";
		
		if (!empty($where))
			$this->query .= " where $where";
		
		//echo '<p>'.$this->query.'</p>'; 
		
		$this->result = mysql_query($this->query, $this->connection) or die(mysql_error());
		return $this->result;
	}
	
	public function executeQuery() {
		if ($this->flagConn == false)
			$this->__construct();
			
		$this->result = mysql_query($this->query, $this->connection);
		return mysql_affected_rows();
	}
	
	public function numRecord() {
		if ($this->flagConn == false)
			$this->__construct();
		
		$this->result = mysql_query($this->query, $this->connection);
		return mysql_num_rows($this->result);
	}
}

?>
